package com.songshuai.webapi.crm.service.impl;

import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.baseapi.dto.feign.response.FunctionInfoResDto;
import com.songshuai.webapi.baseapi.dto.feign.response.NavigationInfoResDto;
import com.songshuai.webapi.crm.constant.CrmConstant;
import com.songshuai.webapi.crm.dao.ConfigFunctionsMapper;
import com.songshuai.webapi.crm.dto.response.account.ConfigFunctionsResDto;
import com.songshuai.webapi.crm.model.ConfigFunctions;
import com.songshuai.webapi.crm.service.ConfigFunctionService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: lhx
 * @Date: 2018/12/28 15:13
 */
@Service
public class ConfigFunctionServiceImpl extends BaseServiceImpl implements ConfigFunctionService {


    @Autowired
    private ConfigFunctionsMapper configFunctionsMapper;


    @Override
    public List<ConfigFunctionsResDto> queryAllList() {
        List<ConfigFunctions> configFunctions = configFunctionsMapper.selectByEntityList(new ConfigFunctions() {{
            setIsDelete(false);
        }});
        if (CollectionUtils.isNotEmpty(configFunctions)){
            //菜单id集合
            List<Integer> navigationIds = configFunctions.stream().filter(f -> CrmConstant.CONFIG_FUNCTIONS_TYPE_NAVIGATION.equals(f.getType()))
                    .map(x -> x.getRelationId()).collect(Collectors.toList());
            //功能id集合
            List<Integer> functionIds = configFunctions.stream().filter(f -> CrmConstant.CONFIG_FUNCTIONS_TYPE_FUNCTION.equals(f.getType()))
                    .map(x -> x.getRelationId()).collect(Collectors.toList());
            Map<Integer, NavigationInfoResDto> navigationMap = new HashMap<>();
            Map<Integer, FunctionInfoResDto> functionMap = new HashMap<>();
            QueryResponse<Map<Integer, NavigationInfoResDto>> navigationInfo = permissionClient.queryNavigationInfo(getBearerToken(),navigationIds);
            QueryResponse<Map<Integer, FunctionInfoResDto>> functionInfo = permissionClient.queryFunctionInfo(getBearerToken(),functionIds);
            if (null != navigationInfo && ResultStatus.SUCCESS.getErrorCode() == navigationInfo.getCode()){
                navigationMap = navigationInfo.getData();
            }
            if (null != functionInfo && ResultStatus.SUCCESS.getErrorCode() == functionInfo.getCode()){
                functionMap = functionInfo.getData();
            }
            if (!navigationMap.isEmpty() && !functionMap.isEmpty()){
                List<ConfigFunctionsResDto> resDtos = configFunctionsMapper.queryAllList();
                disposeConfigFunctions(resDtos,navigationMap,functionMap);
                return resDtos;
            }
        }

        return null;
    }

    /**
     * 递归组装菜单或权限的名称
     * @param resDtos
     * @param navigationMap
     * @param functionMap
     */
    private void disposeConfigFunctions(List<ConfigFunctionsResDto> resDtos,Map<Integer, NavigationInfoResDto> navigationMap,Map<Integer, FunctionInfoResDto> functionMap){
        for (ConfigFunctionsResDto resDto : resDtos) {
            Integer relationId = resDto.getRelationId();
            if (CrmConstant.CONFIG_FUNCTIONS_TYPE_NAVIGATION.equals(resDto.getType())){
                NavigationInfoResDto navigation = navigationMap.get(relationId);
                resDto.setName(navigation.getTitle());
            }else if (CrmConstant.CONFIG_FUNCTIONS_TYPE_FUNCTION.equals(resDto.getType())){
                FunctionInfoResDto function = functionMap.get(relationId);
                resDto.setName(function.getFunctionName());
                resDto.setRemark(function.getRemark());
            }
            if (CollectionUtils.isNotEmpty(resDto.getChildList())){
                disposeConfigFunctions(resDto.getChildList(),navigationMap,functionMap);
            }

        }
    }


}
